﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

namespace SharingCenter.Web.Mod.Groups
{
    using SharingCenter.Web.Classes.Groups;
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes.Requests;
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.SiteEvents;
    using System.Xml.Linq;
    /// <summary>
    /// Summary description for GroupWebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class GroupWebService : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod]
        public void ApproveJoinRequest(string rid)
        {
            if (Context.User.Identity.IsAuthenticated)
            {
                Guid requestId = new Guid(rid);
                Sc_SharedRequest request = RequestOperations.GetRequestById(requestId);
                RequestOperations.ChangeRequestStatus(requestId, RequestStatus.Approved, DateTime.Now);
                Guid groupId = new Guid(request.Context.Element("GroupId").Value);
                GroupOperations.AddUserToGroup(request.RequestorUserId, groupId, true);
                GroupOperations.AddSiteEventOnNewMember(groupId, request.RequestorUserId);
            }
        }

        [WebMethod]
        [ScriptMethod]
        public void RejectJoinRequest(Guid requestId)
        {
            RequestOperations.ChangeRequestStatus(requestId, RequestStatus.Rejected);
        }

        [WebMethod]
        [ScriptMethod]
        public void EnableGroup(string groupId, bool enabled)
        {
            scDataContext cxt;
            Guid gid;
            try
            {
                gid = new Guid(groupId);
            }
            catch { return; }
            Sc_Group g = GroupOperations.GetGroupByGroupId(gid, out cxt);
            g.Approved = enabled;
            try
            {
                cxt.SubmitChanges();
                cxt.Dispose();
            }
            catch { }
            cxt = null;
        }

        [WebMethod]
        [ScriptMethod]
        public void SetHot(string groupId, bool isHot)
        {
            scDataContext cxt;
            Guid gid;
            try
            {
                gid = new Guid(groupId);
            }
            catch { return; }
            Sc_Group g = GroupOperations.GetGroupByGroupId(gid, out cxt);
            g.IsHot = isHot;
            try
            {
                cxt.SubmitChanges();
                cxt.Dispose();
            }
            catch { }
            cxt = null;
        }

        [WebMethod]
        [ScriptMethod]
        public void SetRec(string groupId, bool isRec)
        {
            scDataContext cxt;
            Guid gid;
            try
            {
                gid = new Guid(groupId);
            }
            catch { return; }
            Sc_Group g = GroupOperations.GetGroupByGroupId(gid, out cxt);
            g.IsRecommended = isRec;
            try
            {
                cxt.SubmitChanges();
                cxt.Dispose();
            }
            catch { }
            cxt = null;
                
        }
    }
}
